package core;

import utils.Utils;

public class Message
{
	private String from;
	private String to;
	private String text;
	private String reciveDate;
        private int orderId;
	
	public Message(String _from,String _to,String _text,String _reciveDate,int _orderId)
	{
		from = _from;
		to = _to;
		text = _text;
		reciveDate = _reciveDate;
                orderId = _orderId;
	}
	
	public Message(byte[] data)
	{
		byte[] temp;
		int index,size;
		index = 1;
		//from
		temp = new byte[4];
		for (int i=0;i<4;i++)
		{
			temp[i] = data[index++];
		}
		size = Utils.byteToInt(temp);
		temp = new byte[size];
		for (int i=0;i<size;i++)
		{
			temp[i] = data[index++];
		}
		from = Utils.byteToString(temp);
		//to
		temp = new byte[4];
		for (int i=0;i<4;i++)
		{
			temp[i] = data[index++];
		}
		size = Utils.byteToInt(temp);
		temp = new byte[size];
		for (int i=0;i<size;i++)
		{
			temp[i] = data[index++];
		}
		to = Utils.byteToString(temp);
		//text
		temp = new byte[4];
		for (int i=0;i<4;i++)
		{
			temp[i] = data[index++];
		}
		size = Utils.byteToInt(temp);
		temp = new byte[size];
		for (int i=0;i<size;i++)
		{
			temp[i] = data[index++];
		}
		text = Utils.byteToString(temp);
		//reciveDate
		temp = new byte[4];
		for (int i=0;i<4;i++)
		{
			temp[i] = data[index++];
		}
		size = Utils.byteToInt(temp);
		temp = new byte[size];
		for (int i=0;i<size;i++)
		{
			temp[i] = data[index++];
		}
		reciveDate = Utils.byteToString(temp);
                //orderId
                try
                {
                    temp = new byte[4];
                    for (int i=0;i<4;i++)
                    {
                            temp[i] = data[index++];
                    }
                    orderId = Utils.byteToInt(temp);
                }catch(Exception e){orderId=-1;}
		//������ ������
		temp = null;
		System.gc();

	}
	
	public String getFrom()
	{
		return from;
	}
	
	public String getTo()
	{
		return to;
	}
	
	public String getText()
	{
		return text;
	}
	
	public String getReciveDate()
	{
		return reciveDate;
	}

        public int getOrderId()
        {
            return orderId;
        }

	public byte[] toByte()
	{
		byte[] res,temp;
		int size,index;
		//���������� ������ ������
		size = 1 + 4 + Utils.getStringSize(from)  + Utils.getStringSize(to) + Utils.getStringSize(text) + Utils.getStringSize(reciveDate);
		//������� ������
		res = new byte[size];
		//��������� ������
		index = 0;
		res[index++] = 2;
		//from
		temp = Utils.getStringByte(from);
		for (int i=0;i<temp.length;i++)
		{
			res[index++] = temp[i];
		}
		//to
		temp = Utils.getStringByte(to);
		for (int i=0;i<temp.length;i++)
		{
			res[index++] = temp[i];
		}
		//text
		temp = Utils.getStringByte(text);
		for (int i=0;i<temp.length;i++)
		{
			res[index++] = temp[i];
		}
		//reciveDate
		temp = Utils.getStringByte(reciveDate);
		for (int i=0;i<temp.length;i++)
		{
			res[index++] = temp[i];
		}
                //OrderID
		temp = Utils.int32ToByte(orderId);
		for (int i=0;i<temp.length;i++)
		{
			res[index++] = temp[i];
		}
                
		temp = null;
		System.gc();
		return res;

	}
}
